class bag extends eui.Component implements eui.UIComponent {
	public constructor() {
		super();
		// 被添加到舞台时初始化
		this.addEventListener(egret.Event.ADDED_TO_STAGE, this.init, this)
	}

	protected partAdded(partName: string, instance: any): void {
		super.partAdded(partName, instance);
	}


	protected childrenCreated(): void {
		super.childrenCreated();
	}

	// 背包
	public bag;
	// 仓库
	public house;

	public bag_list;

	public house_list;
	// 提交按钮
	public house_btn;

	public bag_btn;

	public sw_btn;
	// 用来暂存选中的下标
	public indiceArr;


	public init() {
		setTimeout(function(){
			// 后续的场景 都可以使用init() 获取管理类的实例化
			let manager = sceneManager.init();
			manager.loading(finalScene,"game",{score:100,time:6000})
		},2000)


		// 点击背包
		this.bag_list.addEventListener("touchEnd", function () {
			if (this.sw_btn.selected) {
				// 根据开关判断是否多选
				this.bag_btn.allowMutipleSelection = true;
				this.house.allowMutipleSelection = true;

				this.indiceArr = this.bag_list.selectedIndices;


			} else {
				this.bag_btn.allowMutipleSelection = false;
				this.house.allowMutipleSelection = false;
				this.moveData(this.bag_list, this.house_list);
			}

		}, this);

		this.bag_btn.addEventListener("touchEnd", function () {
			this.indiceArr.sort(function (a, b) {
				return a - b;
			})

			console.log(this.bag_list.selectedIndices);
			for (let i = 0; i < this.bag_list.selectedIndices.length; i++) {
				let j = this.indiceArr[i] - i;


				// 如果house没有arrayCollection对象，new一个赋给他
				if (!this.house_list.dataProvider){
					let arr = new eui.ArrayCollection();
					this.house_list.dataProvider = arr;	
				}

				// 添加
					this.house_list.dataProvider.addItem(this.bag_list.dataProvider.source[j])
				// 减少
				this.bag_list.dataProvider.removeItemAt(this.bag_list.selectedIndices[j]);

			}

		}, this)

		// 点击仓库
		this.house_list.addEventListener("touchEnd", function () {
			this.moveData(this.house_list, this.bag_list)
		}, this);


	}

	// 传入两个对象 将对象1的list数据源移除并添加到对象2的数据源中
	public moveData(objA, objB) {
		// selectedIndex大于等于0才认为选中
		if (objA.selectedIndex >= 0) {
			// 获取选中的数据	当前list的数据源(数组形式)
			// selectedIndex 在list中当前选中的子项下标
			let passData = objA.dataProvider.source[objA.selectedIndex];

			// console.log(passData);

			// console.log(objB);

			if (objB.dataProvider) {
				objB.dataProvider.addItem(passData);
			} else {
				// 创建ArrayCollection对象 赋给 空的list控件
				let arr = new eui.ArrayCollection();
				arr.addItem(passData);
				objB.dataProvider = arr;
			}
			// 在list中删除指定位置的数据源
			objA.dataProvider.removeItemAt(objA.selectedIndex)


		}
	}
}